package com.css.zfzx.fwsjcj.modules.taskmanage.task.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.bpm.platform.utils.PlatformPageUtils;
import com.css.zfzx.fwsjcj.cache.DivisionCache;
import com.css.zfzx.fwsjcj.modules.taskmanage.task.entity.FwTaskEntity;
import com.css.zfzx.fwsjcj.modules.taskmanage.task.service.TaskService;
import com.css.zfzx.fwsjcj.modules.taskmanage.task.vo.GroupVo;
import com.css.zfzx.fwsjcj.modules.taskmanage.task.vo.TaskQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/fw/task")
@Slf4j
public class TaskController {
    @Autowired
    TaskService taskService;
    /**
     * 创建任务
     * @param fwTaskEntity
     * @return
     */
    @PostMapping("save")
    public RestResponse saveTask(@RequestBody FwTaskEntity fwTaskEntity){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            taskService.saveTask(fwTaskEntity);
            jsonObject.put("message","保存成功");
            restResponse=RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage="保存失败";
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 分配小组
     * @param groupVo
     * @return
     */
    @PostMapping("update")
    public RestResponse updateTask(@RequestBody GroupVo groupVo){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try {
            taskService.updateTask(groupVo);
            jsonObject.put("message","分配成功");
            restResponse=RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage="分配失败";
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /***
     *批量删除
     * @param ids
     * @return
     */
    @PostMapping("{ids}")
    public RestResponse deletTask(@PathVariable("ids") String ids){
        String[] arr=ids.split(",");
        List<String> list=new ArrayList<>();
        for(int i=0;i<arr.length;i++){
           list.add(arr[i]);
       }
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try {
            taskService.deleteTask(list);
            jsonObject.put("message","删除成功");
            restResponse=RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "删除失败";
            }
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /***
     * 分页查询
     * @param request
     * @param taskQueryVo
     * @return
     */
    @GetMapping("page/{type}")
    public RestResponse getPage(@PathVariable("type") String type, HttpServletRequest request, TaskQueryVo taskQueryVo){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            jsonObject=taskService.getPage(type,request,taskQueryVo,curPage,pageSize);
            restResponse=RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }
    /***
     * 详查采集分页查询
     * @param request
     * @param taskQueryVo
     * @return
     */
    @GetMapping("/taskPage/{type}")
    public RestResponse getTaskPage(@PathVariable("type") String type, HttpServletRequest request, TaskQueryVo taskQueryVo){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            jsonObject=taskService.getPageTask(type,request,taskQueryVo,curPage,pageSize);
            restResponse=RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }
    /***
     * 查看任务详情
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public RestResponse findById(@PathVariable("id") String id){
        RestResponse restResponse=null;
        try{
            FwTaskEntity byId = taskService.findById(id);
            restResponse=RestResponse.succeed(byId);
        }catch (Exception e){
            String errormessage="查看失败";
            log.error(errormessage,e);
           restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 获取所有任务
     * @param request
     * @return
     */
    @GetMapping("/getAllTask")
    public RestResponse getAllTask(HttpServletRequest request){
        RestResponse restResponse=null;
        try{
            List<FwTaskEntity> byId = taskService.getAllTask(request);
            restResponse=RestResponse.succeed(byId);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }
    /**
     * 获取加固任务任务
     * @param request
     * @return
     */
    @GetMapping("/getJGTask")
    public RestResponse getJGTask(HttpServletRequest request){
        RestResponse restResponse=null;
        try{
            List<FwTaskEntity> byId = taskService.getJGTask(request);
            restResponse=RestResponse.succeed(byId);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }
    /**
     * 任务完成
     * @param type
     * @return
     */
    @PostMapping("{type}/complete/{taskId}")
    public RestResponse CompleteTask(@PathVariable("type")String type,@PathVariable("taskId")String taskId){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            taskService.completeTask(type,taskId);
            jsonObject.put("message","修改完成");
            restResponse=RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage="修改失败";
            log.error(errormessage,e);
           restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 获取任务编号
     * @return
     */
    @GetMapping("taskCode")
    public RestResponse getTaskCode(){
        RestResponse restResponse=null;
        try {
            String s = DivisionCache.taskCode();
            restResponse=RestResponse.succeed(s);
        } catch (Exception e) {
            String errormessage="获取失败";
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }

}
